﻿using LitJson;
using System;
using System.Text;

namespace TableCore.Plugin
{
    public class DecimalDataFormatter : IGenFormatter
    {
        StringBuilder buffer = new StringBuilder(30);

        public bool IsValid(string input)
        {
            return true;
        }

        public JsonData Format(GTFile.Content content, string input, string cellName, string comment)
        {
            decimal d = Decimal.Parse(input, System.Globalization.NumberStyles.Float);// Convert.ToDecimal(input.ToString());
            buffer.Clear();
            long l = (long)d;// long.Parse(input, System.Globalization.NumberStyles.AllowExponent);
            bool nig = l < 0;
            if(nig)
            {
                l = -l;
            }
            while (l > 0)
            {
                buffer.Insert(0, l % 10);
                l /= 10;
            }
            if (nig)
                buffer.Insert(0, '-');
            else if (buffer.Length == 0)
                buffer.Append(0);
            return buffer.ToString();
        }
        
        public IExportData ExportData(GTFile.Content content, string input, string cellName, string comment)
        {
            return null;
        }
    }
}
